/**
 * Filename: MusicStatusService.java
 * @author  Firefly
 * @contact postfirefly@163.com
 * @time    2016/5/16 17:10
 */
package com.github.postsanf.yinian.service;

import android.content.Intent;

import com.github.postsanf.yinian.BaseIntentService;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNMember;
import com.github.postsanf.yinian.bean.YNPhoto;
import com.github.postsanf.yinian.bean.YNStatus;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAlbumMemberResponse;
import com.github.postsanf.yinian.entity.YNMusicStatusResponse;
import com.github.postsanf.yinian.entity.YNPhotoResponse;
import com.github.postsanf.yinian.entity.YNStatusResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 音乐相册内容服务
 */
public class MusicStatusService extends BaseIntentService {

    public MusicStatusService() {
        super("MusicStatusService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent != null) {
            final String action = intent.getAction();
            int groupid = intent.getExtras().getInt(ACTIONs.aGroupId);
            switch(action){
                // 获取动态
                case ACTIONs.actionMusicStatus:
                    getMusicStatus(groupid);
                    break;
            }
        }
    }

    // 获取动态
    private void getMusicStatus(int groupId)
    {
        LinkedHashMap<String,String> mParam = new LinkedHashMap<>();
        mParam.put(CommonConstants.YNS_GROUP_ID,String.valueOf(groupId));
        StringRequest req = new StringRequest(getString(R.string.BASE_URL)+getString(R.string.ynShowMusicAlbums))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true){
                    @Override
                    public void onSuccess(String s, Response<String> response){
                        YNMusicStatusResponse resMusicStatus = gson.fromJson(s, YNMusicStatusResponse.class);
                        if (resMusicStatus!=null && ReqUtils.isSuccess(resMusicStatus.getCode())){

                            // 通知动态更新
                            Intent upIntent = new Intent(ACTIONs.actionMusicStatusUpdate);
                            sendBroadcast(upIntent);
                        }
                    }
                    @Override
                    public void onFailure(HttpException e, Response<String> response){
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }
}
